<?php

namespace app\api\controller;

use app\api\model\project\ProjectOrder as ProjectOrderModel;
use app\common\controller\Api;


//服务人员的订单操作
class Projectstafforder extends Api
{

    protected $noNeedRight = ['*'];

    //我的订单
    public function orderlist()
    {
        $order_status = input('order_status','all');

        $where = ['po.staff_user_id'=>$this->auth->id];

        switch ( $order_status ) {
            case "waitservice"://待服务
                $where['po.order_status'] = 1;
                break;
            case "serviceing"://服务中
                $where['po.order_status'] = 2;
                break;
            case "end"://服务结束
                $where['po.order_status'] = 3;
                break;
            case "complete"://已完成
                $where['po.order_status'] = 4;
                break;
            case "other"://其他
                $where['po.order_status'] = ['in',[-1,0,5]];
                break;
        }

        $list = ProjectOrderModel::alias('po')
            ->join('project_staff ps','po.staff_id = ps.id')
            ->where($where)
            ->field('po.*,ps.realname')
            ->order('po.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('提交成功',$list);
    }


    //开始服务
    public function start()
    {
        $order_id = input('order_id',0);

        $order_info = ProjectOrderModel::get($order_id);

        if (!$order_info)$this->error('订单不存在');

        if ($order_info->order_status!=1)$this->error('该状态不可开始服务');

        if ($order_info->order_status==2)$this->error('服务已开始');

        $order_info->order_status = 2;

        $order_info->order_status_name = '服务中';

        $order_info->is_start = 1;

        $order_info->start_time = time();

        if ($order_info->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    //服务结束
    public function end()
    {
        $order_id = input('order_id',0);

        $order_info = ProjectOrderModel::get($order_id);

        if (!$order_info)$this->error('订单不存在');

        if ($order_info->order_status!=2)$this->error('该状态不可结束服务');

        if ($order_info->order_status==3)$this->error('服务已结束');

        $order_info->order_status = 3;

        $order_info->order_status_name = '服务结束';

        $order_info->is_end = 1;

        $order_info->end_time = time();

        if ($order_info->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    //服务完成
    public function complete()
    {
        $order_id = input('order_id',0);

        $order_info = ProjectOrderModel::get($order_id);

        if (!$order_info)$this->error('订单不存在');

        if ($order_info->order_status!=3)$this->error('该状态不可更改订单状态');

        if ($order_info->order_status==4)$this->error('服务已完成');

        $order_info->order_status = 4;

        $order_info->order_status_name = '已完成';

        $order_info->is_complete = 1;

        $order_info->complete_time = time();

        if ($order_info->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }



    //终止服务
    public function termination()
    {
        $order_id = input('order_id',0);
        $order_info = ProjectOrderModel::get($order_id);

        if (!$order_info)$this->error('订单不存在');

        $order_info->order_status = 5;

        $order_info->order_status_name = '已终止服务';

        if ($order_info->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }
}